package cn.edu.wfit.modules.infomanagement.controller;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

import cn.edu.wfit.api.domain.system.SysUser;
import cn.edu.wfit.common.security.utils.SecurityUtils;
import cn.edu.wfit.modules.infomanagement.dto.ImportStudentDto;
import cn.edu.wfit.modules.infomanagement.service.IClsService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.edu.wfit.common.log.annotation.Log;
import cn.edu.wfit.common.log.enums.BusinessType;
import cn.edu.wfit.common.security.annotation.RequiresPermissions;
import cn.edu.wfit.modules.infomanagement.domain.Student;
import cn.edu.wfit.modules.infomanagement.service.IStudentService;
import cn.edu.wfit.common.core.web.controller.BaseController;
import cn.edu.wfit.common.core.web.domain.AjaxResult;
import cn.edu.wfit.common.core.utils.poi.ExcelUtil;
import cn.edu.wfit.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学生管理Controller
 * 
 * @author 韩强
 * @date 2024-11-20
 */
@RestController
@RequestMapping("/student")
public class StudentController extends BaseController
{
    @Autowired
    private IStudentService studentService;
    @Autowired
    private IClsService clsService;

    /**
     * 查询学生管理列表
     */
    @RequiresPermissions("system:student:list")
    @GetMapping("/list")
    public TableDataInfo list(Student student)
    {
        startPage();
        List<Student> list = studentService.selectStudentList(student);
        return getDataTable(list);
    }

    /**
     * 导出学生管理列表
     */
    @RequiresPermissions("system:student:export")
    @Log(title = "学生管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Student student)
    {
        List<Student> list = studentService.selectStudentList(student);
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        util.exportExcel(response, list, "学生管理数据");
    }

    @Log(title = "学生管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:student:import")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file,  ImportStudentDto dto) throws Exception
    {
//        if(Objects.isNull(dto.getClsId())){
//           return AjaxResult.error("导入失败:没有选择班级");
//        }
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        List<Student> userList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getNickAndUsername();
        String message = studentService.importUser(userList, dto.getUpdateSupport(), operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
        util.importTemplateExcel(response, "用户数据");
    }

    /**
     * 获取学生管理详细信息
     */
    @RequiresPermissions("system:student:query")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return success(studentService.selectStudentByUserId(userId));
    }

    /**
     * 新增学生管理
     */
    @RequiresPermissions("system:student:add")
    @Log(title = "学生管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Student student)
    {
        return toAjax(studentService.insertStudent(student));
    }

    /**
     * 修改学生管理
     */
    @RequiresPermissions("system:student:edit")
    @Log(title = "学生管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Student student)
    {
        return toAjax(studentService.updateStudent(student));
    }

    /**
     * 删除学生管理
     */
    @RequiresPermissions("system:student:remove")
    @Log(title = "学生管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(studentService.deleteStudentByUserIds(userIds));
    }
}
